'''
Edited on May 11, 2012

@author: Dan Baskette
'''
 
from BaseMenu import BaseMenu
from Node import Node
from Disk import Disk
from HadoopNodeMenu import HadoopNodeMenu
from HadoopDiskMenu import HadoopDiskMenu

import csv,time

class MainMenu(BaseMenu):
    
    
    def Build(self):
        mainChoice =  -1
        menuItems = ["Manage Node Membership","Create List of Internal Disks","Load Information from CSV","Exit and Begin Install"]
        menuOptions = {1 : self.__OpenHadoopNodeMenu,2 : self.__OpenHadoopDiskMenu,3 : self.__LoadCSVFile}
        while (mainChoice < 0):
            try:
                mainChoice = BaseMenu.BuildMenu(self,"GPHD Install Pre-Configuration",menuItems,menuOptions)
            except:
                mainChoice = -1
        if (int(mainChoice) > 0):
            menuOptions[int(mainChoice)]() 
        else:
            return mainChoice
           

    def __OpenHadoopNodeMenu(self):
        hdnMenu = HadoopNodeMenu(self.Nodes)
        status = -1
        while status <> 0:
            status =  hdnMenu.BuildHadoopNodeMenu()

   
        
    def __OpenHadoopDiskMenu(self):
        hddMenu = HadoopDiskMenu(self.Nodes)
        status = -1
        while status <> 0:
            status =  hddMenu.BuildHadoopDiskMenu()

    def __LoadCSVFile(self):
        print "Load File"
        print "\n****************************************************"
        csvFile = raw_input("\nEnter your Path/filename of CSV Config File : ")
        
        # Currently deletes immediately.  Should test for file validity first
        del self.Nodes[:]

        for row in csv.DictReader(open(csvFile), 'hostname ipaddress personalities disks'.split()):
            
            hostname = row['hostname'] 
            ipAddress = row['ipaddress']     
            personalities = row['personalities']
            node = Node(row['hostname'],row['ipaddress'],row['personalities'])

            for disk in str(row['disks']).split():
                node.AddDisk(Disk(disk))
            self.Nodes.append(node)
       
      
    def exit_Menu(self):
        print "ext"

    def __init__(self,Nodes):
        BaseMenu.__init__(self)
        self.Nodes = Nodes
 
   
        